package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushougeproduct.pojo.param.AttributeAddNewParam;
import cn.tedu.yushougeproduct.pojo.vo.AttributeStandardVO;
import cn.tedu.yushougeproduct.service.IAttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "14. 属性管理模块")
@RequestMapping("/attribute/")
@Slf4j
@Validated
public class AttributeController {
    @Autowired
    IAttributeService attributeService;

    @PostMapping("add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult<Void> addNew(@Valid AttributeAddNewParam attributeAddNewParam) throws Exception {
        log.debug("开始处理【添加属性】的请求，参数：{}", attributeAddNewParam);
        attributeService.addNew(attributeAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("list-by-template")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult<List<AttributeStandardVO>> getStandardById(@Range(min = 1, message = "根据ID查询属性模板详情失败，请提交合法的ID值！")
                                      @RequestParam Long templateId) throws Exception {
        log.debug("开始处理【根据ID查询属性模板详情】的请求，参数：{}", templateId);
        List<AttributeStandardVO> result = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(result);
    }
}
